<%@ page contentType="text/html" pageEncoding="UTF-8"%>
<% response.setContentType("text/html; charset=UTF-8");%>
<% request.setCharacterEncoding("utf-8");%>
<%@ page import="java.sql.*" %>
<%@ page import="java.io.*" %>
<%@ page import="javax.xml.parsers.*" %>
<%@ page import="org.w3c.dom.*" %>
<%@ page import="java.io.File" %>
<%
	Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new File(getServletContext().getRealPath("/") + "web.xml"));
	doc.getDocumentElement().normalize();
	String dbhost = doc.getElementsByTagName("host").item(0).getTextContent();
	String dbport = doc.getElementsByTagName("port").item(0).getTextContent();
	String dbuser = doc.getElementsByTagName("user").item(0).getTextContent();
	String dbpass = doc.getElementsByTagName("pass").item(0).getTextContent();
	String dbname = doc.getElementsByTagName("database").item(0).getTextContent();
	String dbconnString = "jdbc:mysql://" + dbhost + ":" + dbport + "/" + dbname + "?setUnicode=true&characterEncoding=UTF-8";
	Class.forName("com.mysql.jdbc.Driver").newInstance();
	Connection dbconn = DriverManager.getConnection(dbconnString, dbuser, dbpass);
	Statement query = dbconn.createStatement();
%>

<%
	String filename = "files/cauhoi_" + request.getParameter("t") + ".xml";
	String path = getServletContext().getRealPath("/") + filename;
	try {
		OutputStreamWriter buff = new OutputStreamWriter(new FileOutputStream(path), "UTF-8");
		buff.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n<Danhsach>\r\n");
		ResultSet rs = query.executeQuery("select * from cauhoi");
		while (rs.next()) {
			buff.write("<Cauhoi>\r\n");
			buff.write("\t<Noidung>");
			buff.write(rs.getString("noidung"));
			buff.write("</Noidung>\r\n");
			buff.write("\t<Luachon>");
			buff.write(rs.getString("luachon1"));
			buff.write("</Luachon>\r\n");
			buff.write("\t<Luachon>");
			buff.write(rs.getString("luachon2"));
			buff.write("</Luachon>\r\n");
			buff.write("\t<Luachon>");
			buff.write(rs.getString("luachon3"));
			buff.write("</Luachon>\r\n");
			buff.write("\t<Luachon>");
			buff.write(rs.getString("luachon4"));
			buff.write("</Luachon>\r\n");
			buff.write("\t<Dapan>");
			buff.write(Integer.toString(rs.getInt("dapan")));
			buff.write("</Dapan>\r\n");
			buff.write("\t<Dokho>");
			buff.write(Integer.toString(rs.getInt("dokho")));
			buff.write("</Dokho>\r\n");
			buff.write("</Cauhoi>\r\n");
		}
		rs.close();
		buff.write("</Danhsach>\r\n");
		buff.close();
	} catch (Exception e) {
	}
	out.print(filename);
	/*
	try {
		InputStreamReader buff = new InputStreamReader(new FileInputStream("file.txt"), "UTF-8");
		int ch;
		while ((ch = buff.read()) != -1) {
			out.println((char) ch);
		}
		buff.close();
	} catch (Exception e) {
	}
	//*/
%>

<% dbconn.close();%>